package es.masterd.e12_1;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.TimeZone;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;

public class CuentaAtrasWidget extends AppWidgetProvider {
	
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
				
		for(int x=0 ; x < appWidgetIds.length ; x++) {
			// Lo colocamos en una vista remota
			RemoteViews rView = new RemoteViews(context.getPackageName(), R.layout.falta);
			rView.setTextViewText(R.id.falta	, "" + getNumDias() );
			
			// Mandamos la vista al widget
			int id = appWidgetIds[x];
			appWidgetManager.updateAppWidget(id, rView);
		}
	}
	
	protected long getNumDias() {
		GregorianCalendar cal = new GregorianCalendar();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		formatter.setTimeZone(TimeZone.getTimeZone("Europe/Madrid"));
		cal.setTime(formatter.parse("2011-01-01 00:00",new ParsePosition(0)));
			
		long fin = cal.getTime().getTime();
		long ini = new Date().getTime();
		
		return (fin-ini) / (24 * 60 * 60 * 1000); // De milisegundos a d�as

	}
}
